#include <vector>
#include <iostream>
#include <format>
#include <stdexcept>
using namespace std;

// print0, print, print2 是自定义函数，在后续函数章节中会再做介绍
void print0(string&& type, auto&& v) {
  cout << format("{:35}: {}", type, v) << endl;
  // cout << type << ": " << v << endl;
}
void print(string&& type, auto& vev) {
  if (!type.empty()) cout << format("{:35}: ", type);
  for (auto& x: vev) cout << x << ' ';
  cout << endl;
}
void print2(string&& type, auto& vev) {
  cout << type << ":" << endl;
  for (auto& line: vev) {
    for (auto& x: line) {
      cout << x << ' ';
    }
    cout << endl;
  }
}
int main() {
  cout << boolalpha;
  cout << "====== vector 初始化 ======" << endl;
  vector<int> v0(5);
  print("默认初始化", v0);
  vector<int> v1(5, 1);
  print("初始化为 1", v1);
  vector<int> v2{1, 2, 3};
  print("列表初始化", v2);
  vector<int> v3(v1);
  print("vector 初始化", v3);
  vector<int> v4(v2.begin() + 1, v2.end());
  print("指定 vector 范围初始化", v4);

  vector<vector<int>> v5(2, vector<int>(5, 5));
  print2("二维初始化", v5);

  auto v6 = vector(2, vector(5, 6));
  print2("二维 auto 初始化", v6);
  cout << "============ vector 函数 ============" << endl;
  vector<int> v7{1, 2, 3};
  print0("at 0 of [1, 2, 3]", v7.at(0));
  try { // try catch 是捕获异常，如果没有，运行 at(-1) 时会导致程序出错直接结束
    v7.at(1000);
  } catch (const std::out_of_range& error) {
    print0("at 100 of [1, 2, 3]", "out of range");
  }
  print0("[0] of [1, 2, 3]", v7[0]);
  print0("[1000] of [1, 2, 3]", v7[1000]);
  print0("back of [1, 2, 3]", v7.back());
  print0("front of [1, 2, 3]", v7.front());
  cout << format("{:35}: ", "迭代器循环");
  for (auto iter = v7.begin(); iter != v7.end(); iter ++) {
    cout << *iter << ' '; // 注意，输出的时候需要加 *
  }
  cout << endl;
  print0("size of [1, 2, 3]", v7.size());
  print0("empty of [1, 2, 3]", v7.empty());
  vector<int> v8;
  print0("empty of []", v8.empty());
  v7.push_back(4);
  print("after push_back 4 in [1, 2, 3]", v7);
  v7.pop_back();
  print("after pop_back in [1, 2, 3, 4]", v7);
  v7.resize(2);
  print("after resize(2) in [1, 2, 3]", v7);
  v7.resize(3);
  print("after resize(3) in [1, 2]", v7);
  v7.resize(4, 1);
  print("after resize(4, 1) in [1, 2, 0]", v7);
  v7.clear();
  print("after clear [1, 2, 0, 1]", v7);
  v7 = {1, 2, 3};
  v7.insert(v7.begin() + 1, 2);
  cout << "after insert(begin() + 1, 2) of [1, 2, 3]:" << endl;
  print("", v7);
  v7.insert(v7.begin() + 2, {4, 5});
  cout << "after insert(begin() + 2, {4, 5}) of [1, 2, 2, 3]:" << endl;
  print("", v7);
  v7.insert(v7.end(), v2.begin() + 1, v2.end());
  cout << "after insert(end(), {1, 2, 3}(begin() + 1, end())) of [1, 2, 4, 5, 2, 3]:" << endl;
  print("", v7);
  v7.erase(v7.begin());
  cout << "after erase(begin()) of [1, 2, 4, 5, 2, 3, 2, 3]" << endl;
  print("", v7);
  v7.erase(v7.begin() + 1, v7.begin() + 5);
  cout << "after erase(begin()+1, begin()+5) of [2, 4, 5, 2, 3, 2, 3]" << endl;
  print("", v7);
  cout << "============ vector 运算 ============" << endl;
  vector<int> v9;
  v9 = v7;
  print("= vector<int>{2, 2, 3}", v9);
  vector<int> v10{2, 2, 4};
  vector<int> v11{1, 2, 3};
  print0("[2, 2, 3] == [2, 2, 3]", v9 == v7);
  print0("[2, 2, 3] < [2, 2, 4]", v9 < v10);
  print0("[2, 2, 3] > [1, 2, 3]", v9 > v11);
}